﻿using System;
using System.Diagnostics;
using System.Threading;

namespace LMSGridLibrary
{
    public static class UsageMonitor
    {
        private static readonly Thread MonThread = new Thread(UpdadeData);
        /// <summary>
        /// Time in miliseconds used to wait between two consecutives measurements
        /// </summary>
        public static int TickTime=250;

        /// <summary>
        /// Cpu usage ratio 0-100
        /// </summary>
        public static float CpuUsage{get; private set; }

        /// <summary>
        /// Free memory in MB
        /// </summary>
        public static float FreeMem{get; private set; }

        /// <summary>
        /// Disk Read Ratio in B/s
        /// </summary>
        public static float DiskRead{get; private set; }

        /// <summary>
        /// Disk Write Ratio in B/s
        /// </summary>
        public static float DiskWrite{get; private set; }

        /// <summary>
        /// Start the monitor thread to update values
        /// </summary>
        public static void Start()
        {
            MonThread.SetApartmentState(ApartmentState.MTA);
            MonThread.Priority = ThreadPriority.Highest;
            MonThread.Start();
        }

        public static void Abort()
        {
            MonThread.Abort();
        }

        static readonly PerformanceCounter CpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        static readonly PerformanceCounter MemCounter = new PerformanceCounter("Memory", "Available MBytes");
        static readonly PerformanceCounter DiskReadCounter = new PerformanceCounter("PhysicalDisk", "Avg. Disk Bytes/Read", "_Total");
        static readonly PerformanceCounter DiskWriteCounter = new PerformanceCounter("PhysicalDisk", "Avg. Disk Bytes/Write", "_Total");


        static void UpdadeData()
        {
            while(true)
            {
                switch (Environment.OSVersion.Platform)
                {
                    case PlatformID.Unix:
                        
                        break;
                    case PlatformID.MacOSX:
                        Console.WriteLine("Install a decent OS");
                        break;
                    case PlatformID.Win32NT:
                    case PlatformID.Win32S:
                    case PlatformID.Win32Windows:
                    case PlatformID.WinCE:
                    case PlatformID.Xbox:
                        CpuUsage = CpuCounter.NextValue();
                        FreeMem = MemCounter.NextValue();
                        DiskRead = DiskReadCounter.NextValue();
                        DiskWrite = DiskWriteCounter.NextValue();
                        Console.WriteLine("cpu:                  " + CpuUsage + " %");
                        Console.WriteLine("free mem:             " + FreeMem + "MB");
                        Console.WriteLine("disk Read Bytespsec:  " + DiskRead + "B/s");
                        Console.WriteLine("disk Write Bytespsec: " + DiskWrite + "B/s");
                        break;
                }
                Thread.Sleep(TickTime);
            }
        }
    }
}
